package com.jdog.chat.contrainer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.jdog.chat.entity.Client;

public class ClientUtil {

	/** 单例模式 **/
	private static ClientUtil instance = null;
	private ClientUtil() {}
	public static ClientUtil getInstance() {
       if(instance == null) {
    	   instance = new ClientUtil();
       }
       return instance;
    }
	
	private static CopyOnWriteArrayList<Client> clients = new CopyOnWriteArrayList<Client>();
	
	public int getClientsCount() {
		return clients.size();
	}
	
	public void add(Client client) {
		clients.add(client);
	}
	
	public Client getClientByUsername(String username) {
		Client client = null;
		for(Client c : clients) {
			if(c.getUsername().equals(username)) {
				client = c;
				break;
			}
		}
		return client;
	}
	
	public CopyOnWriteArrayList<Client> getClientsList() {
		return clients;
	}
	
	
}
